<?php

class Action
{
    private $per_page = 30;

    /*
     * 查询所有合伙人简报信息
     * */
    public function get_all_partner_bulletin(){
        $page_no  = $search_filter = $is_excel = null;

        extract(URLTracker::init(array(
            'page_no' => 1,
            'search_filter' => array(),
            'is_excel' => 0,
        )));

        $relation = array();
        $relation['is_enable'] = '1';
        // 合伙人姓名的过滤
        if (isset($search_filter['partner_name']) && $search_filter['partner_name']) {
            $relation['`partner_name` LIKE '] = '%' . $search_filter['partner_name'] . '%';
        }

        $relation_count = _model('partner_bulletin')->getTotal($relation);

        if ($relation_count) {
            $pager = new Pager($this->per_page);
            if ($pager->generate($relation_count)) {
                Response::assign('pager', $pager);
            }
            $device_by = ' ORDER BY `id` DESC ';
            if ($is_excel != 1) {
                $device_by .= $pager->getLimit($page_no);
            }

            $relation_info = _model('partner_bulletin')->getList($relation, $device_by);
            foreach($relation_info as $k => $v){
                $province_info = area_helper::get_area_info(@ $v['province_code']);
                $city_info = area_helper::get_area_info(@ $v['city_code']);
                $area_manager_name = user_helper::get_realname_by_id($v['area_manager_id']);
                if($province_info){
                    $relation_info[$k]['province_info'] = $province_info['area'];
                }else{
                    $relation_info[$k]['province_info'] = "";
                }
                if($city_info){
                    $relation_info[$k]['city_info'] = $city_info['area'];
                }else{
                    $relation_info[$k]['city_info'] = "";
                }
                if($area_manager_name){
                    $relation_info[$k]['area_manager_name'] = $area_manager_name;
                }else{
                    $relation_info[$k]['area_manager_name'] = "";
                }
            }
            if ($is_excel == 1) {
                $this->export_excel($relation_info);
            }
            Response::assign('count', $relation_count);

        }

        if (empty($relation_info)) {
            $relation_info = array();
        }

        Response::assign('partner_bulletin_list', $relation_info);
        Response::assign('search_filter', $search_filter);

        Response::display('admin/partner_bulletin_list.html');
    }


    /**
     * 合伙人简报信息
     */
    public function export_excel($list = array()) {
        $device_list = array();

        $device_lists = array();
        foreach ($list as $k => $v) {
            $device_lists[] = $v;
        }
        $lines = array();
        foreach ($device_lists as $k => $v) {
            array_push($lines, array(
                $v['partner_id'],
                $v['partner_name'],
                $v['province_info'],
                $v['city_info'],
                $v['area_manager_name'],
                $v['store_num'],
                $v['store_order_rate'],
                $v['is_bind_device_store_num'],
                $v['all_order_num'],
                $v['prepare_pay_order_num'],
                $v['prepare_delivery_order_num'],
                $v['in_delivery_order_num'],
                $v['finish_delivery_order_num'],
                $v['cancel_order_num'],
                $v['store_stock_goods_num'],
                $v['store_sell_goods_num'],
                $v['add_time'],
                $v['update_time'],
            ));
        }
        //初始化excel对象
        $objPHPExcel = new PHPExcel();
        $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
        //设置当前的sheet索引，用于后续的内容操纵
        $objPHPExcel->setActiveSheetIndex(0);
        $objActSheet = $objPHPExcel->getActiveSheet();
        // 哪几列
        $col_arr = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R');
        // 第一排的标题
        $col_title_arr = array(
            '合伙人ID',
            '合伙人姓名',
            '所在省份',
            '所在城市',
            '所属大区经理',
            '掌柜数量',
            '掌柜下单率',
            '设备总和',
            '订单总数',
            '待收款',
            '待配送',
            '配送中',
            '配送完',
            '撤销',
            '进货总量(箱)',
            '出货总量(箱)',
            '添加时间',
            '更新时间',
        );

        foreach ($col_arr as $k => $v) {
            //设置单位格内容A1,B1,C1,D1
            $objActSheet->setCellValue($v . "1", $col_title_arr[$k]);
        }
        //填充单元格
        $key = 2;
        foreach ($device_lists as $k => $v) {
            foreach ($col_arr as $k2 => $v2) {
                $place = $v2 . $key;
                $objActSheet->setCellValue($place, $lines[$k][$k2]);
            }
            $key++;
        }

        //设置页面类型
        header("Content-Type: application/vnd.ms-excel");
        //用于下载的文件头
        header("Content-Type: application/force-download");
        header("Content-Type: application/download");
        header("Content-Type: application/octet-stream");

        //$title = trim($catebirthday_gift_recordry_name."表").".xls";
        $title = date("Y-m-d")."合伙人简报统计.xls";
        // ff正常，ie不正常，下面是从网站找到的解决方案
        if (preg_match('/MSIE/', $_SERVER['HTTP_USER_AGENT'])) {
            $title = rawurlencode($title);
        }
        header("Content-Disposition: attachment; filename=" . $title);
        header("Content-Transfer-Encoding: binary");
        //最后修改时间
        header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
        //不使用缓存
        header('Cache-Control: ust-revalidate, post-check=0, pre-check=0');
        header('Cache-Control: public');
        header("Expires: 0");
        header("Pragma: public");
        $objWriter->save('php://output');
        exit;
    }
}
